﻿#region " LICENSE "
// DRILL - Dependency Resolution Interface Layer Library
// Copyright (c) 2008-2012 Wade A. Reynolds. All rights reserved.
// http://drill.codeplex.com/
// 
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
// 
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
#endregion
using System;
using System.Collections.Generic;
using System.Web;
using Drill.Core;
using Drill.Extensibility;
using Drill.Integration.Web.Mocks;
using Drill.LocatorPattern;
using Drill.TestSupport;
using Drill.TestSupport.Mocks.MockDrillBits;
using NUnit.Framework;

namespace Drill.Integration.Web
{
	[TestFixture]
	public class AspNetDrillChuckDependencyResolverWithSingleDrillBitTests : BaseDrillBitTests
	{
		protected override IDependencyResolver GetDependencyResolverInstanceToTest()
		{
			return new AspNetDrillChuckDependencyResolver(
					new DependencyResolverInitContext
					{
						Name = "Default"
					  , DrillBits = 
							new List<IDrillBit>
								{
									new SimpleDomainAndHttpContextBaseMockDrillBit()
								}
					}
				);
		}

		[Test]
		public void CanConstructUsingConstructorWithInitializationContextArgument()
		{
			//Arrange
			// Nothing to arrange.

			// Act
			AspNetDrillChuckDependencyResolver dependencyResolver =
				new AspNetDrillChuckDependencyResolver(
					new DependencyResolverInitContext
					{
						Name = "Default"
					  , DrillBits =
							new List<IDrillBit>
								{
									new AutomobileDomainMockDrillBit()
								  , new SimpleDomainAndHttpContextBaseMockDrillBit()
								}
					}
				);

			// Assert
			Assert.That(dependencyResolver, Is.Not.Null);
		}

		[ExpectedException(typeof(ArgumentNullException), ExpectedMessage = "initContext", MatchType = MessageMatch.Contains)]
		[Test]
		public void CannotConstructUsingConstructorWithNullInitializationContextArgument()
		{
			//Arrange
			IDependencyResolverInitContext context = null;

			// Act
			// ReSharper disable ObjectCreationAsStatement
			// ReSharper disable ExpressionIsAlwaysNull
			new AspNetDrillChuckDependencyResolver(context);
			// ReSharper restore ExpressionIsAlwaysNull
			// ReSharper restore ObjectCreationAsStatement

			// Assert
			// Nothing to assert because the test should throw an expected exception (see ExpectedException attribute).
		}

		[ExpectedException(typeof(ArgumentException), ExpectedMessage = "initContext.DrillBits property is null or empty", MatchType = MessageMatch.Contains)]
		[Test]
		public void CannotConstructUsingConstructorWithInitializationContextArgumentWithNullDrillBits()
		{
			//Arrange
			IDependencyResolverInitContext context =
				new DependencyResolverInitContext { Name = "Default", DrillBits = null };

			// Act
			// ReSharper disable ObjectCreationAsStatement
			new AspNetDrillChuckDependencyResolver(context);
			// ReSharper restore ObjectCreationAsStatement

			// Assert
			// Nothing to assert because the test should throw an expected exception (see ExpectedException attribute).
		}

		[ExpectedException(typeof(ArgumentException), ExpectedMessage = "initContext.DrillBits property is null or empty", MatchType = MessageMatch.Contains)]
		[Test]
		public void CannotConstructUsingConstructorWithInitializationContextArgumentWithEmptyDrillBits()
		{
			//Arrange
			IDependencyResolverInitContext context =
				new DependencyResolverInitContext { Name = "Default", DrillBits = new List<IDrillBit>() };

			// Act
			// ReSharper disable ObjectCreationAsStatement
			new AspNetDrillChuckDependencyResolver(context);
			// ReSharper restore ObjectCreationAsStatement

			// Assert
			// Nothing to assert because the test should throw an expected exception (see ExpectedException attribute).
		}

		[Test]
		public void CanConstructUsingConstructorWithInitializationContextAndILocatorOfHttpContextBaseArguments()
		{
			//Arrange
			// Nothing to arrange.

			// Act
			AspNetDrillChuckDependencyResolver dependencyResolver =
				new AspNetDrillChuckDependencyResolver(
					new DependencyResolverInitContext
					{
						Name = "Default"
					  , DrillBits =
							new List<IDrillBit>
								{
									new AutomobileDomainMockDrillBit()
								  , new SimpleDomainAndHttpContextBaseMockDrillBit()
								}
					}
			  , new HttpContextLocator(new HttpContextMock())
				);

			// Assert
			Assert.That(dependencyResolver, Is.Not.Null);
		}

		[ExpectedException(typeof(ArgumentNullException), ExpectedMessage = "initContext", MatchType = MessageMatch.Contains)]
		[Test]
		public void CannotConstructUsingConstructorWithNullInitializationContextAndILocatorOfHttpContextBaseArguments()
		{
			//Arrange
			IDependencyResolverInitContext context = null;
			ILocator<HttpContextBase> locator = new HttpContextLocator(new HttpContextMock());


			// Act
			// ReSharper disable ObjectCreationAsStatement
			// ReSharper disable ExpressionIsAlwaysNull
			new AspNetDrillChuckDependencyResolver(context, locator);
			// ReSharper restore ExpressionIsAlwaysNull
			// ReSharper restore ObjectCreationAsStatement

			// Assert
			// Nothing to assert because the test should throw an expected exception (see ExpectedException attribute).
		}

		[Test]
		public void CanConstructUsingConstructorWithInitializationContextAndNullILocatorOfHttpContextBaseArguments()
		{
			//Arrange
			IDependencyResolverInitContext context =
				new DependencyResolverInitContext
				{
					Name = "Default"
				  , DrillBits =
						new List<IDrillBit>
							{
								new AutomobileDomainMockDrillBit()
							  , new SimpleDomainAndHttpContextBaseMockDrillBit()
							}
				};
			ILocator<HttpContextBase> locator = null;

			// Act
			// ReSharper disable ObjectCreationAsStatement
			// ReSharper disable ExpressionIsAlwaysNull
			AspNetDrillChuckDependencyResolver dependencyResolver =
				new AspNetDrillChuckDependencyResolver(context, locator);
			// ReSharper restore ExpressionIsAlwaysNull
			// ReSharper restore ObjectCreationAsStatement

			// Assert
			Assert.That(dependencyResolver, Is.Not.Null);
		}

		[Test]
		public void CanGetInstanceOfHttpContextBase()
		{
			// Arrange
			IDependencyResolver dependencyResolver = GetDependencyResolverInstanceToTest();

			// Act
			HttpContextBase httpContext = dependencyResolver.GetInstance(typeof(HttpContextBase)) as HttpContextBase;

			// Assert
			Assert.That(httpContext, Is.Not.Null);
			Assert.That(httpContext, Is.AssignableFrom<HttpContextMock>());
		}

		[Test]
		public void CanGetInstanceGenericOfHttpContextBase()
		{
			// Arrange
			IDependencyResolver dependencyResolver = GetDependencyResolverInstanceToTest();

			// Act
			HttpContextBase httpContext = dependencyResolver.GetInstance<HttpContextBase>();

			// Assert
			Assert.That(httpContext, Is.Not.Null);
			Assert.That(httpContext, Is.AssignableFrom<HttpContextMock>());
		}
	}
}